/**
 * @file luogu/2922/main.cpp
 * @brief
 * @see
 * @author Ruiming Guo (guoruiming@stu.scu.edu.cn)
 * @copyright 2022
 * @date 2022/6/22 09:26:13
 **/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
struct msg {
  int ch[2], sum, end;
} T[500001];
int A[10001];
int main() {
  // High rating and good luck!
  int m, n, sz = 0;
  cin >> m >> n;
  for (int i = 1, len; i <= m; ++i) {
    cin >> len;
    int u = 0;
    for (int j = 1, b; j <= len; ++j) {
      cin >> b;
      int &v = T[u].ch[b];
      if (v == 0) v = ++sz;
      u = v, T[u].sum++;  // 经过此节点的信号增加了
    }
    T[u].end++;  // 在此节点结束的信号增加
  }
  for (int i = 1, len; i <= n; ++i) {
    cin >> len;
    for (int j = 1; j <= len; ++j) cin >> A[j];
    int u = 0, ans = 0;
    bool match = true;
    for (int j = 1; j <= len; ++j) {
      if (T[u].ch[A[j]] != 0)
        u = T[u].ch[A[j]];
      else {
        match = false;
        break;
      }
      ans += T[u].end;  // 情形一
    }
    if (match) ans += T[u].sum - T[u].end;  // 情形三
    cout << ans << endl;
  }
  return 0;
}
